﻿#include"LoginModel.h"
#include"XQSettingsInfo.h"
#include"XQUserData.h"
#include<QDebug>
//表名
#define USERTABLENAME "User"
LoginModel::LoginModel()
{
}

QList<XQLoginAccount> LoginModel::loginAccountsSettings()
{
	QList<XQLoginAccount> list;
	if (settings() == nullptr)
		return list;
	auto accounts = settings()->loginAccounts();
	//qDebug() << accounts;
	for (auto&data: accounts)
	{
		list <<XQLoginAccount(data);
	}
	return list;
}

QString LoginModel::loginCurrentAccountSettings()
{
	if (settings() == nullptr)
		return QString();
	return settings()->currentAccount();
}

XQDatabaseConnectionModel LoginModel::databaseConnectionSettings()
{
	if (settings() == nullptr)
		return XQDatabaseConnectionModel();
	return settings()->databaseConnectionModel();
}

void LoginModel::saveDatabaseConnectionSettings(const XQDatabaseConnectionModel& model)
{
	if (settings() == nullptr)
		return ;
	settings()->setDatabaseConnectionModel(model.toMap());
}

void LoginModel::saveLoginAccounts(const QList<XQLoginModel>& data)
{
	if (settings() == nullptr)
		return;
	QList<QVariantMap> list;
	for (auto&a:data)
	{
		//qDebug() << a.toVariantMap();;
		list << a.toMap();
	}
	settings()->setLoginAccounts(list);
}

void LoginModel::saveLoginCurrentAccountSettings(const QString& account)
{
	if (settings() == nullptr)
		return;
	settings()->setCurrentAccount(account);
}

void LoginModel::initDatabaseTable()
{
	auto mysql = this->mysql();
	//qDebug() << "初始化表";
	/*qInfo() << "查询表是否存在,不存在将创建";*/
	//QString tableName = "User";
	if (!mysql->tabelExists(USERTABLENAME))
	{
		if (!mysql->createTable(XQUserData(), USERTABLENAME))
		{//创建表失败

		}
	}
	if (mysql->findData(USERTABLENAME, "uuid", "`account`=?", {"root"}).isEmpty())
	{
		//创建默认账号
		XQUserData user;
		user.setAccount("root");
		user.setPassword("123456");
		user.setName("root");
		user.setUuid(mysql->uuid());
		user.setPermission(XQUserData::Permission::root);
		mysql->INSERT(USERTABLENAME, user.toMap(user.fields()));
	}
}

QPixmap LoginModel::userePortraitMySql(const QString& account)
{
	auto mysql = this->mysql();
	auto data = mysql->findDatas_toMap(USERTABLENAME, { "portrait_data","portrait_format"}, "`account`=?", {account});
	if(data.isEmpty())
		return QPixmap();
	return XQUserData(data.first(), false).portrait();
}

XQUserData LoginModel::loginUserMySql(const QString& account, const QString& password)
{
	auto mysql = this->mysql();
	auto data = mysql->findDatas(USERTABLENAME, XQUserData::Fields(), "`account`=? AND `password`=?", { account,password });
	if(data.isEmpty())
		return XQUserData();
	return XQUserData(data.first(),false);
}

bool LoginModel::registerAccountMySql(const XQUserData& user)
{
	auto mysql = this->mysql();
	if (mysql->findData(USERTABLENAME, "uuid", "`account`=?", { QString("%1").arg(user.account()) }).isEmpty())
	{
		XQUserData u = user;
		u.setUuid(mysql->uuid());
		u.setPermission(XQUserData::Permission::user);
		if(mysql->INSERT(USERTABLENAME, u.toMap(u.fields())))
			return true;
	}
	return false;
}
